<template>
  <div>
    <router-view />
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";
import qs from "qs";
import wx from "weixin-js-sdk";
import { Toast, Dialog } from "vant";
import wxJSFn from "@/js/wxJSFn";
const publicPath = require("../../config/publicPath");
import { shareConfig, aid, shareImg } from "@/config/shareConfig";
export default {
  data() {
    return {};
  },
  computed: {
    appData() {
      return this.$store.state.appData;
    },
  },
  watch: {
    appData: {
      deep: true,
      immediate: true,
      handler(val, oldVal) {
        console.log("appDataChange", val, oldVal);
        if (val && val.resQuery && val.jsApiTicket && val.resQuery.code) {
          this.initData(val.resQuery, val.jsApiTicket);
        }
      },
    },
  },
  methods: {
    ...mapActions({
      getShopsById: "activity/getShopsById",
      queryIsLottery: "activity/queryIsLottery",
      getEnroll: "activity/getEnroll",
    }),
    ...mapMutations({
      setShopsMsg: "activity/setShopsMsg",
      setDrawNum: "activity/setDrawNum",
      setEnrollMsg: "activity/setEnrollMsg",
    }),
    async initData(resQuery, jsApiTicket) {
      if (!resQuery.sid) {
        return this.getShopsError();
      }
      this.setShare();
      let shopsRes = await this.getShopsById({
        id: parseInt(resQuery.sid),
        page: 1,
        limit: 1,
      });
      console.log(shopsRes);
      if (shopsRes && shopsRes.code == 0) {
        let cur = {};
        for (let i = 0; i < shopsRes.data.list.length; i++) {
          if (resQuery.sid == shopsRes.data.list[i].id) {
            cur = shopsRes.data.list[i];
            break;
          }
        }
        this.setShopsMsg(cur);
      } else {
        return this.getShopsError();
      }
      this.getEnroll({
        ac_id: aid,
        open_id: this.$store.state.userInfo.openId,
      })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    setShare() {
      wxJSFn.showShareMenu();
      wxJSFn.share({
        ...shareConfig,
        link: `${publicPath}index.html?sid=${this.appData.resQuery.sid}`,
        imgUrl: shareImg,
      });
    },
    getShopsError() {
      Dialog.confirm({
        title: "提示",
        message: "获取门店信息失败，请重新打开页面",
        showCancelButton: false,
      }).then(() => {
        // on confirm
        wx.closeWindow();
      });
    },
  },
};
</script>

<style>
</style>